<?php
namespace App\Admin\Controllers\Traits\WxNoteTraits;

use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formNoteTrait{

    public function index_config(Content $content)
    {
        return $content->title('恋爱小纸条配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_config()->render()}</div>
HTML
            );
    }

    protected function form_config(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->tab('基本', function (Form $form) {

            $form->table('app_note_middle_box','中间按钮', function (NestedForm $table) {
                $table->image('img', '图片')->url('files/uploads')->uniqueName()->autoUpload()->required();
                $table->text('text', '标题')->placeholder('抽纸条')->required();
                $table->radio('type', '类型')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target', '跳转id')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->default(Settings::get('app_note_middle_box', ''));

            $form->divider();

            $form->table('app_note_bottom_box','底部按钮', function (NestedForm $table) {
                $table->image('img', '图片')->url('files/uploads')->uniqueName()->autoUpload()->required();
                $table->text('title', '标题')->placeholder('邀请好友')->required();
                $table->text('desc', '描述')->placeholder('赚钱利器');
                $table->radio('type', '类型')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target', '跳转id')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->default(Settings::get('app_note_bottom_box', ''));

        });

        $form->action('note/action/config');
        return $form;
    }

    public function config(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('app_note_bottom_box', AdminUtils::_table_data($request->input('app_note_bottom_box')), true);
        Settings::set('app_note_middle_box', AdminUtils::_table_data($request->input('app_note_middle_box')), true);


        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
